package org.example.service;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.example.dao.QuestionInfoDao;
import org.example.entity.QuestionImportDTO;
import org.example.entity.QuestionInfoEntity;
import org.springframework.stereotype.Service;

import java.time.LocalDateTime;
import java.util.HashSet;
import java.util.List;
import java.util.stream.Collectors;

@Service
public class QuestionInfoServiceImpl extends ServiceImpl<QuestionInfoDao, QuestionInfoEntity>
        implements QuestionInfoService {
    //根据考试ID获取所有题目
    private  QuestionInfoDao questionInfoDao;
    public List<QuestionInfoEntity> getQuestionsByExamId(int examId) {
        QueryWrapper<QuestionInfoEntity> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("exam_id", examId);
        return baseMapper.selectList(queryWrapper);
    }

    @Override
    public void removeDuplicates(Integer examId) {

    }

    @Override
    public QuestionInfoEntity addQuestion(QuestionInfoEntity question) {
        this.save(question);
        return question;
    }

    @Override
    public boolean deleteQuestionById(Long questionId) {
        return this.removeById(questionId);
    }

    @Override
    public QuestionInfoEntity updateQuestion(QuestionInfoEntity question) {
        this.updateById(question);
        return question;
    }

    @Override
    public QuestionInfoEntity getQuestionById(Long questionId) {
        return this.getById(questionId);
    }

    @Override
    public IPage<QuestionInfoEntity> getAllQuestions(Page<QuestionInfoEntity> page) {
        return this.page(page);
    }

    @Override
//    public IPage<QuestionInfoEntity> searchQuestionsBySubject(String subject, Page<QuestionInfoEntity> page) {
//        return this.lambdaQuery()
//                .eq(QuestionInfoEntity::getSubject, subject)
//                .page(page);
//    }
    public IPage<QuestionInfoEntity> searchQuestionsBySubject(String subject, Page<QuestionInfoEntity> page) {
        // 添加日志输出，查看查询的参数
        System.out.println("Searching for subject: " + subject);

        IPage<QuestionInfoEntity> result = this.lambdaQuery()
                .eq(QuestionInfoEntity::getSubject, subject)
                .page(page);

        // 输出查询结果的记录数
        System.out.println("Total records found: " + result.getTotal());
        return result;
    }
    @Override
    public IPage<QuestionInfoEntity> searchQuestionsByKnowledgeAndType(String knowledge, String type, Page<QuestionInfoEntity> page) {
        return this.lambdaQuery()
                .eq(QuestionInfoEntity::getKnowledgePoint, knowledge)
                .eq(QuestionInfoEntity::getQuestionType, type)
                .page(page);
    }

    @Override
    public IPage<QuestionInfoEntity> searchQuestionsByDifficulty(byte difficulty, Page<QuestionInfoEntity> page) {
        return this.lambdaQuery()
                .eq(QuestionInfoEntity::getQuestionDifficulty, difficulty)
                .page(page);
    }
    public void importQuestions(List<QuestionImportDTO> questionList) {
        List<QuestionInfoEntity> entities = questionList.stream().map(dto -> {
            QuestionInfoEntity entity = new QuestionInfoEntity();
            entity.setExamId((int) dto.getExamId());
            entity.setContent(dto.getContent());
            entity.setQuestionDifficulty(dto.getQuestionDifficulty());
            entity.setQuestionType(dto.getQuestionType());
            entity.setOptions(dto.getOptions());
            entity.setContentAnswer(dto.getContentAnswer());
            entity.setScore((float) dto.getScore());
            entity.setKnowledgePoint(dto.getKnowledgePoint());
            entity.setSubject(dto.getSubject());
            entity.setCreateTime(LocalDateTime.now());
            entity.setUpdateTime(LocalDateTime.now());
            return entity;
        }).collect(Collectors.toList());

        this.saveBatch(entities);
    }
    public void removeDuplicates(Long examId) {
        // 查询指定考试的所有试题
        QueryWrapper<QuestionInfoEntity> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("exam_id", examId);
        List<QuestionInfoEntity> questions = questionInfoDao.selectList(queryWrapper);

        // 使用 HashSet 判断是否重复
        HashSet<String> uniqueQuestions = new HashSet<>();
        List<QuestionInfoEntity> duplicates = questions.stream()
                .filter(q -> !uniqueQuestions.add(q.getContent() + "|" + q.getOptions())) // 按内容+选项判断
                .collect(Collectors.toList());

        // 删除重复数据
        duplicates.forEach(q -> questionInfoDao.deleteById(q.getQuestionId()));
    }
}
